import req from "./request.js";
import { ElMessage } from "element-plus";

/**
 * 查询数据字典
 * @param {*} params
 * @returns
 */
export const getDataDict = async (params) => {
  return await req.post("/datadict/get", params).then(({ data }) => {
    if (data.code == 0) {
      return data;
    } else {
      ElMessage({
        message: data.msg,
        type: "error",
      });
    }
  });
};

/**
 * 查询数据字典明细
 * @param {*} params
 * @returns
 */
export const getDataDictDetail = async (params) => {
  return await req
    .post("/datadictdetail/findDataDictAndDetail", params)
    .then(({ data }) => {
      if (!data.data || data.data.length == 0) {
        ElMessage({
          message: "暂无数据",
          type: "error",
        });
        return;
      }
      return data;
    });
};

/**
 * 保存数据字典明细
 * @param {*} params
 * @returns
 */
export const saveDataDictDetail = async (params) => {
    const action = params.id ?'update':'save'
    return await req
      .post(`/datadictdetail/${action}`, params)
      .then(({ data }) => {
        if (!data.data) {
          ElMessage({
            message: "暂无数据",
            type: "error",
          });
          return;
        }
        return data;
      });
  };
